﻿using System;
using Microsoft.SharePoint;

namespace SKN
{
    /// <summary>
    /// 
    /// </summary>
    public static class PropertyBag
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyCollection"></param>
        /// <param name="web"></param>
        public static void SetPropertyBag(SPFeaturePropertyCollection propertyCollection, SPWeb web)
        {
            // Loop through each property in the collection  
            foreach (SPFeatureProperty property in propertyCollection)
            {
                // Validate the name and value are populated  
                if ((!string.IsNullOrEmpty(property.Name)) && (!string.IsNullOrEmpty(property.Value)))
                {
                    // Check if the property name already exists  
                    if (web.Properties.ContainsKey(property.Name))
                    {
                        // Update property with new value  
                        web.Properties[property.Name] = property.Value;
                    }
                    else
                    {
                        // Add new property with name and value  
                        web.Properties.Add(property.Name, property.Value);
                    }
                }
            }

            // Update PropertyBag  
            web.Properties.Update();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyCollection"></param>
        /// <param name="web"></param>
        public static void ResetPropertyBag(SPFeaturePropertyCollection propertyCollection, SPWeb web)
        {
            // Loop through each property in collection  
            foreach (SPFeatureProperty property in propertyCollection)
            {
                try
                {
                    // If the property Name exists, try to set it's corresponding propertybag value to ""  
                    if (!String.IsNullOrEmpty(property.Name))
                    {
                        web.Properties[property.Name] = string.Empty;
                    }
                }
                catch
                {
                    throw;
                }
            }
            web.Properties.Update();
        }


        //private void SetListPropertyBag(SPFeaturePropertyCollection propertyCollection, SPWeb web, string listname)
        //{
        //    SPList list = web.Lists[listname];

        //    // Loop through each property in the collection  
        //    foreach (SPFeatureProperty property in propertyCollection)
        //    {
        //        // Validate the name and value are populated  
        //        if ((!string.IsNullOrEmpty(property.Name)) && (!string.IsNullOrEmpty(property.Value)))
        //        {
        //            // Check if the property name already exists  
        //            if (list.Properties.ContainsKey(property.Name))
        //            {
        //                // Update property with new value  
        //                web.Properties[property.Name] = property.Value;
        //            }
        //            else
        //            {
        //                // Add new property with name and value  
        //                web.Properties.Add(property.Name, property.Value);
        //            }
        //        }
        //    }

        //    // Update PropertyBag  
        //    web.Properties.Update();
        //}

        //private void ResetListPropertyBag(SPFeaturePropertyCollection propertyCollection, SPWeb web)
        //{
        //    // Loop through each property in collection  
        //    foreach (SPFeatureProperty property in propertyCollection)
        //    {
        //        try
        //        {
        //            // If the property Name exists, try to set it's corresponding propertybag value to ""  
        //            if (!String.IsNullOrEmpty(property.Name))
        //            {
        //                web.Properties[property.Name] = string.Empty;
        //            }
        //        }
        //        catch
        //        {
        //        }
        //    }
        //    web.Properties.Update();
        //}  
    }
}